﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Com.Qkds.Erp.WebUi
{
    public partial class AdminEdit : System.Web.UI.Page
    {
        public int pid
        {
            get
            {
                return int.Parse(ViewState["pid"].ToString());
            }
            set
            {
                ViewState["pid"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pid = 0;
                string tablename = "role";
                this.ddlrole.DataSource = new roleBLL().Select(tablename);
                this.ddlrole.DataValueField = "ID";
                this.ddlrole.DataTextField = "roleName";
                this.ddlrole.DataBind();
                if (Request["id"] != null)
                {
                    string TableName = "Users";
                    pid = int.Parse(Request["id"].ToString());
                    UsersModel model = new AdminBLL().GetModelByID(TableName, pid);
                    this.txtUser.Value = model.Name;
                    this.txtPwd.Value = model.Pwd;
                    this.ddlrole.SelectedValue = model.roleID.ToString();

                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UsersModel model = new UsersModel()
            {
                Name = this.txtUser.Value,
                Pwd = this.txtPwd.Value,
                roleID = int.Parse(this.ddlrole.SelectedValue),

            };
            AdminBLL bll = new AdminBLL();
            if (pid > 0)
            {
                bll.Gai(model, pid);

            }
            else
            {
                bll.Add(model);


            }
            Response.Redirect("admin.aspx");
        }
    }
}